package com.mall.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.commons.utils.PageUtils;
import com.mall.dubbo.api.UmsResourceApi;
import com.tanhua.model.domain.UmsResource;
import com.tanhua.model.dto.ResourceListDao;
import com.tanhua.model.vo.PageVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UmsResourceService {

    @DubboReference
    private UmsResourceApi umsResourceApi;

    //分页模糊查询后台资源
    public PageVo listResource(ResourceListDao resourceListDao) {

        Page page = umsResourceApi.listResource(resourceListDao);

        if (page == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return PageUtils.getPageVo(page);
    }

    //根据ID删除后台资源
    public void delete(Integer id) {
        boolean exits = umsResourceApi.delete(id);
        if (!exits) {
            throw new BusinessException(ErrorResult.EmaError());
        }
    }

    //修改后台资源
    public void update(Integer id, UmsResource umsResource) {
        boolean exits = umsResourceApi.update(id, umsResource);
        if (!exits) {
            throw new BusinessException(ErrorResult.EmaError());
        }
    }

    //添加后台资源
    public void create(UmsResource umsResource) {
        boolean exits = umsResourceApi.create(umsResource);
        if (!exits) {
            throw new BusinessException(ErrorResult.resourceError());
        }
    }

    //查询所有后台资源
    public List<UmsResource> listAll() {
        List<UmsResource> resource = umsResourceApi.listAll();
        if (resource == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return resource;
    }
}
